package club.zhilin.guli.service.base.config;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhilin
 * @since 2022/1/11 16:18
 */
@Component
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket frontendConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("frontendApi")
                .apiInfo(frontendInfo())
                .select()
                .paths(Predicates.and(PathSelectors.regex("/portal/.*")))
                .build()
                .securityContexts(securityContexts())
                .securitySchemes(securitySchemes());
    }

    @Bean
    public Docket backendConfig() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("backendApi").apiInfo(backendApiInfo()).select().paths(Predicates.and(PathSelectors.regex("/admin/.*"))).build();
    }

    private ApiInfo frontendInfo() {
        return new ApiInfoBuilder().title("谷粒学院前台Api文档")
                .description("描述了谷粒学院的所有前台接口定义")
                .version("1.0")
                .contact(new Contact("zhilin", "https://www.zhilin.club", "1537372358@qq.com"))
                .build();
    }

    /**
     * 添加摘要信息
     *
     * @return apiInfo
     */
    private ApiInfo backendApiInfo() {
        return new ApiInfoBuilder().title("谷粒学院后台Api文档")
                .description("描述了谷粒学院的所有后台接口定义")
                .version("1.0")
                .contact(new Contact("zhilin", "https://www.zhilin.club", "1537372358@qq.com"))
                .build();
    }

    /**
     * 安全模式，这里指定token通过Authorization头请求头传递
     */
    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeyList = new ArrayList<>();
        apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
        return apiKeyList;
    }

    /**
     * 安全上下文
     */
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build());
        return securityContexts;
    }

    /**
     * 默认的安全上引用
     */
    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }
}
